// miniprogram/pages/Class_manage/index.js
const db=wx.cloud.database({})
const tb_leave=db.collection('Leave')
const tb_course=db.collection('Course')
const tb_courseSales=db.collection('CourseSales')
Page({

  /**
   * 页面的初始数据
   */
  data: {
  leave:[],
  class:[],
  sign:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    this.getLeave()
  },
onPullDownRefresh:function(){
this.setData({
  leave:[],
  class:[],
  sign:[]
})
this.getLeave()
},
getLeave:function(){   let that=this;
  tb_leave.where({
     status:0
  }).get({
   success:res=>{
   that.setData({
     leave:res.data
   })
   for(var i=0;i<res.data.length;i++)
   tb_course.doc(res.data[i].course_id).get({
     success:re=>{
       this.data.class.push(re.data)
       console.log(1)
       this.setData({
         class:this.data.class
       })
     }
   })
   }
 }
 )},

  agree:function(e){
    console.log(e.currentTarget.dataset.value.course_id)
    tb_courseSales.where({
      studentID:e.currentTarget.dataset.value.student_id,
      courseId:e.currentTarget.dataset.value.course_id
    }).get({
      success:res=>{
        let sign=new Array()
        sign=res.data[0].sign
        sign[res.data[0].sign.length-1]=2
       tb_courseSales.doc(res.data[0]._id).update({
         data:{
            sign:sign
         }
      })

      tb_leave.doc(e.currentTarget.dataset.value._id).update({
        data:{
        status:1
        },
        success:function(res){
          wx.hideLoading()
          wx.showToast({
            title: '同意成功',
          })
        },
        fail:function(res){
          wx.hideLoading()
          wx.showToast({
            title: '同意失败',
            icon:"none"
          })
        }
      })
      }
    })
  },
  disagree:function(e){
    tb_leave.doc(e.currentTarget.dataset.value._id).update({
      data:{
        status:2
      },
      success:function(res){
        wx.hideLoading()
        wx.showToast({
          title: '拒绝成功',
        })
      },
      fail:function(res){
        wx.hideLoading()
        wx.showToast({
          title: '拒绝失败',
          icon:"none"
        })
      }
    })
    
  }
})